package com.enfo.zjabank.dao;

import com.enfo.zjabank.tool.DateUtil;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zhoum on 2016/8/24.
 */
@Repository
public class ChangeDaoImpl extends BaseDao implements ChangeDao {
    @Override
    public Map<String,String> getColumnNames(String prpId) {
        String sql = "select prp_id,pfield from efPropertyApple where prp_id in ("+prpId+")";
        final Map<String,String> colomnNameMap = new LinkedHashMap<>();
        this.jdbcTemplate.query(sql, new RowCallbackHandler(){
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                colomnNameMap.put(rs.getString("pfield"),rs.getString("prp_id"));
            }
        });
        return colomnNameMap;
    }

    @Override
    public void addChange(final Map<String, String> form, final Map<String, String> columnNameMap) {
        final String today = DateUtil.getToday();
        final StringBuilder columns = new StringBuilder("debt_id,modify_date,modify_state");
        final StringBuilder params = new StringBuilder("?,?,?");
        for(Map.Entry<String,String> entry:columnNameMap.entrySet()){
            columns.append("," + entry.getKey());
            params.append(",?");
        }
        String sql = "insert into buz_escape_modify ("+columns.toString()+") values ("+params.toString()+")";
        this.jdbcTemplate.update(sql,new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1,form.get("id"));
                ps.setString(2,today);
                ps.setString(3,form.get("modify_state"));
                int i = 4;
                for(Map.Entry<String,String> entry:columnNameMap.entrySet()){
                    ps.setString(i,form.get(entry.getValue()));
                    i++;
                }
            }
        });
    }

    @Override
    public void modifyState(Map<String, String> form) throws Exception {
        //获得当前修改状态
        String modifyStateSql = "select modify_state from buz_escape_debts where debt_id=?";
        //更新修改状态修改时间修改原因的sql语句
        String updateSql = "update buz_escape_debts set modify_state=?,modify_date=?,modify_reason=? where debt_id=?";
        Object[] updateParams = new Object[4];
        String modifyState = this.jdbcTemplate.queryForObject(modifyStateSql,new Object[]{form.get("id")},String.class);
        switch (modifyState){
            case "1010":
                updateParams[0] = "1020";
                break;
            case "1030":
                updateParams[0] = "1050";
                break;
            default:
                throw new Exception();
        }
        //添加到map中后面insert"修改表"时使用(modify_state需两表保持一致)
        form.put("modify_state", (String) updateParams[0]);
        updateParams[1] = DateUtil.getToday();
        updateParams[2] = form.get("modify_reason");
        updateParams[3] = form.get("id");
        this.jdbcTemplate.update(updateSql,updateParams);
        //同步信息到修改表
        String syncSql = "update buz_escape_modify a,buz_escape_debts b set a.area_code=b.area_code,a.check_dept=b.check_dept where a.debt_id=b.debt_id and a.debt_id=?";
        this.jdbcTemplate.update(syncSql,new Object[]{form.get("id")});
    }
}
